package com.xposed.aliproxy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xposed.aliproxy.MainActivity;
import com.xposed.aliproxy.R;
import com.xposed.aliproxy.http.HttpManager;
import com.xposed.aliproxy.thread.CheckNewVersionThread;
import com.xposed.aliproxy.utils.ParamConfig;
import com.xposed.aliproxy.utils.ProgressUtil;
import com.xposed.aliproxy.utils.Utils;
import com.xposed.aliproxy.utils.XposeUtil;

import org.json.JSONObject;

public class LogInActivity extends BaseActivity {
    private EditText edt_agentId, edt_agentKey, edt_aliAccount;

    private ParamConfig paramConfig;
    private String agentId, agentKey, aliAccount;

    @Override
    public void loadLayout() {
        setContentView(R.layout.activity_login);
    }

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setTitleStr("登录");
        this.paramConfig = ParamConfig.getInstance(this.context);
        this.edt_agentId = (EditText) findViewById(R.id.login_edittext_agentId);
        this.edt_agentKey = (EditText) findViewById(R.id.login_edittext_agentKey);
        this.edt_aliAccount = (EditText) findViewById(R.id.login_edittext_aliaccount);
        TextView textView = (TextView) findViewById(R.id.login_textview_version);
        this.agentId = this.paramConfig.getString("agentId", "");
        this.agentKey = this.paramConfig.getString("agentKey", "");
        this.aliAccount = this.paramConfig.getString("aliAccount", "");
        if (!Utils.isNull(this.agentId)) {
            this.edt_agentId.setText(this.agentId);
        }
        if (!Utils.isNull(this.agentKey)) {
            this.edt_agentKey.setText(this.agentKey);
        }
        if (!Utils.isNull(this.aliAccount)) {
            this.edt_aliAccount.setText(this.aliAccount);
        }
        setTitleRight("设置", new OnClickListener() {
            @Override
            public void onClick(View v) {
                LogInActivity.this.startActivity(new Intent(LogInActivity.this.context, SetIPActivity.class));
            }
        });
        String versionName = HttpManager.getVersionName(this);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("版本 ");
        stringBuilder.append(versionName);
        textView.setText(stringBuilder.toString());


        findViewById(R.id.login_button_login).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        new CheckNewVersionThread(this).start();
        XposeUtil.xposedLog("deviceID->", XposeUtil.getDeviceID(this.context));
        XposeUtil.xposedLog("deviceIMEI->", XposeUtil.getImei(this.context));
        XposeUtil.xposedLog("deviceMac->", XposeUtil.getMac());
    }

    /**
     * 登录
     */
    private boolean login() {
        final String agentId = "CA000043";
        final String agentKey = "6dd5491c0619405f860f5aa753e74a7f";
        final String aliAccount = "15630701153";

//        final String agentId = this.edt_agentId.getText().toString().trim();
//        final String agentKey = this.edt_agentKey.getText().toString().trim();
//        final String aliAccount = this.edt_aliAccount.getText().toString().trim();
        if (Utils.isNull(agentId)) {
            Toast.makeText(this.context, "请输入代理商编号", Toast.LENGTH_SHORT).show();
            return false;
        } else if (Utils.isNull(agentKey)) {
            Toast.makeText(this.context, "请输入代理商密钥", Toast.LENGTH_SHORT).show();
            return false;
        } else if (Utils.isNull(aliAccount)) {
            Toast.makeText(this.context, "请输入账号", Toast.LENGTH_SHORT).show();
            return false;
        } else if (this.paramConfig.getString("host", null) == null) {
            Toast.makeText(this.context, "请先设置网络参数", Toast.LENGTH_SHORT).show();
            return false;
        } else {
            ProgressUtil.showProgressDialog(this.context, false, "登录...");
            HttpManager.requestLogin(this, agentId, agentKey, aliAccount, new HttpManager.RequestCallback() {
                @Override
                public void onSuccess(JSONObject jSONObject) {
                    if (!(LogInActivity.this.agentId.equals(agentId) && LogInActivity.this.agentKey.equals(agentKey) && aliAccount.equals(LogInActivity.this.aliAccount))) {
                        LogInActivity.this.paramConfig.putString("agentId", agentId);
                        LogInActivity.this.paramConfig.putString("agentKey", agentKey);
                        LogInActivity.this.paramConfig.putString("aliAccount", aliAccount);
                    }
                    Toast.makeText(LogInActivity.this.context, "登录成功", Toast.LENGTH_SHORT).show();
                    LogInActivity.this.startActivity(new Intent(LogInActivity.this.context, MainActivity.class));
                    LogInActivity.this.finish();
                }

                @Override
                public void onFailMsg(String str, String str2) {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(str);
                    stringBuilder.append("-");
                    stringBuilder.append(str2);
                    Toast.makeText(LogInActivity.this.context, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFinish() {
                    ProgressUtil.closeProgressDialog();
                }
            });
            return true;
        }
    }
}
